from datetime import datetime

from app.database import db


# 用户模型
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(120), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return f"<User {self.username}>"

    def check_password(self, password):
        # 这里可以使用 Flask-Bcrypt 或其它工具进行密码验证
        return self.password == password


# 项目信息模型
class ProjectInfo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    project_name = db.Column(db.String(256), unique=True, nullable=False)
    src_path = db.Column(db.String(512), nullable=True)
    report_path = db.Column(db.String(512), nullable=True)
    token_path = db.Column(db.String(512), nullable=True)
    ast_path = db.Column(db.String(512), nullable=True)
    pdg_path = db.Column(db.String(512), nullable=True)
    csg_path = db.Column(db.String(512), nullable=True)
    file_model_path = db.Column(db.String(512), nullable=True)
    sen_model_path = db.Column(db.String(512), nullable=True)
    username = db.Column(db.String(80), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return f"<Project {self.project_name}>"


# 历史信息表
class HistoryInfo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), nullable=False)
    project_name = db.Column(db.String(256), nullable=False)
    result = db.Column(db.String(512), nullable=True)
    score = db.Column(db.Float, nullable=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return f"<History {self.id}>"


# 文件推荐列表
class FileRecommend(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    project_name = db.Column(db.String(256), nullable=False)
    file_name = db.Column(db.String(256), nullable=False)
    file_path = db.Column(db.String(512), nullable=False)
    score = db.Column(db.Float, nullable=False)

    def __repr__(self):
        return f"<FileRecommend {self.id}>"


# 语句推荐列表
class SenRecommend(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    project_name = db.Column(db.String(256), nullable=False)
    file_path = db.Column(db.String(512), nullable=False)
    sentence = db.Column(db.String(512), nullable=False)
    score = db.Column(db.Float, nullable=False)

    def __repr__(self):
        return f"<FileRecommend {self.id}>"


# 评价指标
class Metric(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    project_name = db.Column(db.String(256), nullable=False)
    met_type = db.Column(db.String(32), nullable=False)
    top1 = db.Column(db.Float, nullable=False)
    top5 = db.Column(db.Float, nullable=False)
    top10 = db.Column(db.Float, nullable=False)
    mrr_score = db.Column(db.Float, nullable=False)
    map_score = db.Column(db.Float, nullable=False)

    def __repr__(self):
        return f"<Metric {self.id}>"
